package com.ys.code.generator.model;

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.WordUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * <p>@description: </p>
 *
 * @author yangshuang
 * @version 1.0
 * @date 2019/1/1 17:56
 */
@Getter
@Setter
public class TableColumn {
    private String table;
    private String column;
    private String type;
    private String keyType;

    private String columnName;


    public TableColumn(String table, String column, String type, String keyType, String columnName) {
        this.table = table;
        this.column = column;
        this.type = type;
        this.keyType = keyType;
        this.columnName = columnName;
    }


    public boolean isPk() {
        return StringUtils.isNotBlank(this.keyType) && StringUtils.equalsIgnoreCase("PRI", this.keyType);
    }

    public String getJavaType() {
        switch (this.type) {
            case "int":
            case "tinyint":
            case "smallint":
                return "Integer";
            case "varchar":
            case "char":
                return "String";
            case "bigint":
                return "Long";
            case "decimal":
                return "Double";
            case "timestamp":
            case "date":
            case "datetime":
                return "Date";

        }
        return "String";
    }


    /**
     * 名称替换
     *
     * @return
     */
    public String getUppercaseAttributeName() {
        if (column == null) {
            return null;
        }
        return WordUtils.capitalizeFully(column, new char[]{'_'}).replace("_", "");
    }

    /**
     * 属性字段
     *
     * @return
     */
    public String getAttributeName() {
        return StringUtils.uncapitalize(getUppercaseAttributeName());
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }
}
